package com.wowangz.cms.dailyoperation.note.api;

import java.rmi.server.ServerCloneException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wowangz.cms.common.utils.SysConstant;
import com.wowangz.cms.component.datagrid.pojo.DataGrid;
import com.wowangz.cms.component.datagrid.pojo.DataResult;
import com.wowangz.cms.component.datagrid.pojo.DataStyle;
import com.wowangz.cms.component.datagrid.pojo.PangingParam;
import com.wowangz.cms.component.datagrid.utils.DataTypeConstant;
import com.wowangz.cms.component.datagrid.utils.PangingTools;
import com.wowangz.cms.dailyoperation.note.entity.DemandNoteClazz;
import com.wowangz.cms.dailyoperation.note.service.DemandNoteService;
import com.wowangz.cms.syscore.IssTransactionContainer;
import com.wowangz.cms.syscore.api.BaseController;
import com.wowangz.cms.syscore.api.JSONFactory;
import com.wowangz.cms.syscore.exception.ServiceException;
import com.wowangz.cms.syscore.utils.ServletUtils;

@SuppressWarnings("unchecked")
@Scope("prototype")
@Controller
public class DemandNoteController extends BaseController {

	private static final String PATH = "/WEB-INF/jsp/";

	public String getPath(String path) {
		return PATH + path;
	}

	/**
	 * 获得Service服务信息
	 * 
	 * @param flag
	 *            是否开启事务
	 * @return
	 */
	private DemandNoteService getService(boolean flag) {
		return (DemandNoteService) IssTransactionContainer.getInstance(DemandNoteService.class, flag);
	}

	@RequestMapping(value = "/dailyoperation/demandNote/list.web", method = RequestMethod.GET)
	public String list(HttpServletRequest request) throws ServiceException {
		return getPath("dailyoperation/demandNote/list");
	}

	/**
	 * 获得注册页面信息的列表，得到注册商户的列表数据
	 * 
	 * @param request
	 * @param response
	 */

	@RequestMapping(value = "/dailyoperation/demandNote/getJson.json", method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody void getJson(HttpServletRequest request, HttpServletResponse response) {
		try {
			PangingParam pangingParam = (PangingParam) ServletUtils.getParametersToBaseEntity(request, PangingParam.class);
			Map<String, Object> map = new HashMap<String, Object>();
			map = (Map<String, Object>) ServletUtils.getParametersToHashMap(request);
			DataGrid dataGrid = new DataGrid();
			dataGrid.setPangingParam(pangingParam);
			map.put("startrow", dataGrid.getStartrow());
			map.put("endrow", dataGrid.getRows());
			map.put("sort", dataGrid.getSort());
			String name = request.getParameter("name");
			if (StringUtils.isNotEmpty(name)) {
				map.put("name", "%" + name + "%");
			}
			map.put("order", dataGrid.getOrder());
			DataResult dataResult = getDataResult(pangingParam, map);
			if (pangingParam.getReportExportType() == 0) {
				response.getWriter().write(JSONFactory.toJSONString(dataResult, userContext));
			}
		} catch (Exception e) {
			throw new ServiceException("查询数据异常", new Throwable());
		}
	}

	/**
	 * 获得一个 DataResult对象
	 * 
	 * @param pangingParam
	 * @param map
	 * @return
	 * @throws ServerCloneException
	 */
	private DataResult getDataResult(PangingParam pangingParam, Map<String, Object> map) throws ServerCloneException {
		map.put("statusid", SysConstant.YesOrNo.YES);
		long resultCount = getService(false).findResultCount(map);
		List<DemandNoteClazz> demandNoteClazzs = getService(false).findResultList(map);
		DataStyle dataStyle = new DataStyle();
		dataStyle.setPageIndex(pangingParam.getPage());
		dataStyle.setDataCount(resultCount);
		dataStyle.setDataList(demandNoteClazzs);
		dataStyle.addDepict("storeid", DataTypeConstant.LONG);
		dataStyle.addDepict("name", DataTypeConstant.STRING);
		dataStyle.addDepict("storeNumber", DataTypeConstant.STRING);
		dataStyle.addDepict("liquidationcycle", DataTypeConstant.STRING);
		dataStyle.addDepict("payamount", DataTypeConstant.AMOUNT_2);
		dataStyle.addDepict("amount", DataTypeConstant.AMOUNT_2);
		dataStyle.addDepict("starttime", DataTypeConstant.DATE);
		dataStyle.addDepict("endtime", DataTypeConstant.DATE);
		dataStyle.addDepictByFunction("statusid", SysConstant.RecordStatus.class, "getName", new Class[] { long.class });
		dataStyle.addDepictByFunction("chargestatus", SysConstant.ChargeStatus.class, "getName", new Class[] { long.class });
		dataStyle.addDepict("id", DataTypeConstant.LONG);
		DataResult dataResult = PangingTools.buildResultPagerInfo(dataStyle);
		return dataResult;
	}

}
